const utils = require("../../../utils/utils");
const app = getApp()
/**
 * 缩放
 * @param {number} ow 源图宽 
 * @param {number} oh 源图高
 * @param {number} tw 目标宽
 * @param {number} th 目标高
 */
function contain(ow, oh, tw, th) {
    let s = 0;
    if (ow > oh) {
      s = ow / oh;
      ow = tw;
      oh = ow / s
    }
    else {
      s = ow / oh;
      oh = th;
      ow = oh * s
    }
  
    if (th < oh) {
      s = ow / oh;
      oh = th;
      ow = oh * s
    }
  
    if (tw < ow) {
      s = ow / oh;
      ow = tw;
      oh = ow / s
    }
    return { ow, oh }
  }
Page({
  data: {
    /** 底部安全距离 */
    safeAreaBottom: 0,
    /** 图片地址 */
    imageUrls: [],
    /** 排版 */
    composingArr: [],
    /** 纸张尺寸 */
    paperSize: null,
    /** 最大上传图片数 */
    maxUploadImageCount: 9,
    /** 纸张名称 */
    paperSizeName: 'A4'
  },
  onLoad(options) {
    if (options.sizeName) this.setData({ paperSizeName: options.sizeName })
    var that = this;
    wx.setNavigationBarTitle({
      title:options.typeName?options.typeName:'拼版'
    })
    that.setData({
      imageUrls:JSON.parse(options.imgArray)?JSON.parse(options.imgArray):'',
      code:options.code?options.code:'',
      typeName:options.typeName?options.typeName:'拼版',
      serviceMainId:options.serviceMainId,
      serviceSubtypeId:options.serviceSubtypeId,
      tagFloor:options.tagFloor
    })
    wx.getSystemInfo({
      success: (result) => {
        var model = result.model;
        if (model == 'iPhone X') {
          that.setData({
            pdbottom: '68',
            lineH: 128
          })
        } else {
          that.setData({
            pdbottom: '40',
            lineH: 100
          })
        }
      },
    })
    if (options.sizeName) that.setData({
      paperSizeName: options.sizeName
    })
    if (getApp().isLowerThenVersion("2.20.1")) {
      const systemInfo = wx.getSystemInfoSync();
      that.setData({
        safeAreaBottom: systemInfo.screenHeight - systemInfo.safeArea?.bottom ?? systemInfo.screenHeight
      })
    } else {
      const windowInfo = wx.getWindowInfo()
      that.setData({
        safeAreaBottom: windowInfo.screenHeight - windowInfo.safeArea.bottom
      })
    }

  },
  //选择照片方式
  chooseStyle() {
    var that = this;
    wx.showActionSheet({
      itemList: ['相册', '拍照', '从QQ会话选择'],
      success(res) {
        if (res.tapIndex == 0) {
          that.getPhoto('album');
        }
        if (res.tapIndex == 1) {
          that.getPhoto('camera');
        }
        if (res.tapIndex == 2) {
          that.getPhoto1();
        }
      },
      fail(res) {

      }
    })
  },
  getPhoto1(){
    wx.chooseMessageFile({
        count: 9,
        type: 'image',
        success:(res)=>{
          this.setData({ imageUrls: [...this.data.imageUrls, ...res.tempFiles.map(f => f.path)] })
            // var array = [];
            // for (var i = 0; i < res.tempFiles.length; i++) {
            //     array.push(res.tempFiles[i].path)
            // }
            // that.setData({
            //     chooseImageArray: array
            // })
            // //轮巡判断上传
            // that.uploadFileImg(that.data.chooseImageArray[0], 0);
        }
    })
  },
  getPhoto(type){
    if (getApp().isLowerThenVersion("2.10.0")) {
      wx.chooseImage({
        count: this.data.maxUploadImageCount - this.data.imageUrls.length,
        sizeType:['original'],
        sourceType:[type],
        success: (res) => {
          this.setData({ imageUrls: [...this.data.imageUrls, ...res.tempFiles.map(f => f.path)] })
        }
      })
    }
    else {
      wx.chooseMedia({
        count: this.data.maxUploadImageCount - this.data.imageUrls.length,
        mediaType: ['image'],
        sourceType:[type],
        sizeType:['original'],
        success: (res) => {
          this.setData({ imageUrls: [...this.data.imageUrls, ...res.tempFiles.map(f => f.tempFilePath)] })
        }
      })
    }
  },
  addPhotos() {
    if (this.data.imageUrls.length >= 9) {
      utils.toast('最多只能选择9张',2000)
      return false;
    }
    this.chooseStyle()
  },
  removePhoto(e) {
    this.data.imageUrls.splice(e.detail.index, 1)
    this.setData({
      imageUrls: this.data.imageUrls
    })
  },
  composingChange(e) {
    const paperSize = e.detail.paperSize
    setTimeout(() => {
      wx.createSelectorQuery().select('.container').boundingClientRect().exec((res) => {
        let { windowWidth } = wx.getSystemInfoSync()
        let [con] = res
        let w = paperSize.w / 750 * windowWidth;
        let h = paperSize.h / 750 * windowWidth;
        let cw = con.width - 20
        let ch = con.height - 40
        let { ow, oh } = contain(w, h, cw, ch)
        ow = ow / windowWidth * 750;
        oh = oh / windowWidth * 750;
        paperSize.w = ow;
        paperSize.h = oh;
        this.setData({ composingArr: e.detail.composing, paperSize })
      })
    }, 0);
  },
  async composePhoto() {
    if(this.data.imageUrls.length==0){
        utils.toast('请选择图片后再进行此操作')
        return false;
    }
    this.selectComponent("#loading").showLoadingCus('加密上传中');
    let result = await this.selectComponent('.main').toDataUrl('',1)
    this.uploadFileImg(result.tempFilePath)
  },
  uploadFileImg(url) {
    var that = this;
    utils.uploadfile(url, url, '', '', '', function (res) {
        var data = JSON.parse(res.data).data
        that.selectComponent("#loading").hideLoadingCus();
        wx.redirectTo({
          url: '../list/list?code=' + that.data.code + '&name=' + that.data.typeName + '&path=' + data.printUrl + '&pageNum=' + data.pageTotal +'&fileUrl='+data.fileUrl+'&serviceMainId='+that.data.serviceMainId+'&serviceSubtypeId='+that.data.serviceSubtypeId+'&tagFloor='+that.data.tagFloor+'&color='+data.color
        })
    })
},
})